package com.gxkj.movie.controllers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gxkj.movie.config.utils.OkHttpCli;
import com.gxkj.movie.controllers.seo.IndexSeo;
import com.gxkj.movie.helpers.*;
import com.gxkj.movie.threads.BaiduSubmitUrlThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author sqyc
 */
@Controller
@Slf4j
public class KnowsSharePlatformController extends BaseController {


    @Autowired
    private BaiduSpiderHelper baiduSpiderHelper;

    @Autowired
    private RecommendSiteHelpper recommendSiteHelpper;


    private static Map<String,JSONArray> cache = new HashMap<>();

    String[] colors = new String[]{
            "/static/img/color/blue.png",
            "/static/img/color/green.png"
//            ,"/static/img/color/pink.png"
            ,"/static/img/color/orange.png"
//            ,"/static/img/color/red.png"

    };
    String pink = "/static/img/color/pink.png";

    @GetMapping("knowsshareplatform")
    public String index(HttpServletRequest req, Model model,@RequestParam(value = "forceclear" ,required=false,defaultValue ="false") Boolean forceclear){



        String basePath = req.getScheme()+"://"+req.getServerName()+"/knowsshareplatform";
       System.out.println("basePath="+basePath);
        String[] param = {
                //需要推送的网址
                basePath
        };

        new Thread(new BaiduSubmitUrlThread(baiduSpiderHelper,param)).start();


        if(forceclear){
            cache.clear();
        }
        renderHeader(  req,   model);
        renderSharePlatFormProd(req,model,"dev_waibao",null);


        //推荐视频
        renderRecommendVod( req,   model);
        //新闻提示
        renderNews(req,   model,null);
//        renderJdProd(req,   model,  "taobao_nvbao","淘宝热销女包");
//        renderJdProd(req,   model,  "taobao_nvzhuang","淘宝热销女装");
//
//        renderJdProd(req,   model,  "taobao_nvxie","淘宝热销女鞋");
//        renderJdProd(req,   model,  "jd_pro","京东热销商品");
//        renderJdProd(req,   model,  "jd_huodong","京东最近热门活动");



        return getTempletate(req)+"/knowsshareplatformv3";
    }

    @Override
    public void renderHeader(HttpServletRequest req, Model model){
        super.renderHeader(req,model);
        model.addAttribute("menu", MenuEnum.DevShare.getKey());
        model.addAttribute("TITLE", IndexSeo.KnowsSharePlatformController_TITLE);
        model.addAttribute("KEYWORDS",IndexSeo.KnowsSharePlatformController_KEYWORDS);
        model.addAttribute("DESCRIPTION",IndexSeo.KnowsSharePlatformController_DESCRIPTION);
        model.addAttribute("SITE_NAME", IndexSeo.SITE_NAME);
    }

    public void  renderSharePlatFormProd(HttpServletRequest req, Model model,String sitetype,String country){
        JSONArray list = null;

        String tag = sitetype+(StringUtils.isEmpty(country)?"":country);
        if(cache.get(tag) == null){
            synchronized (this){
                if(cache.get(tag) == null){
                    JSONObject pager = recommendSiteHelpper.findRecommendVodData(1,60,sitetype,country);
                    log.info( JSON.toJSONString(pager));
                    if(pager != null) {
                        list = pager.getJSONArray("list");
                    }
                    if(list != null ){
                        JSONObject item = null;
                        int length = colors.length;
                        int r = 0;


                        for(int i=0;i<list.size();i++){
                            item = list.getJSONObject(i);
                            r = i%length;

                            item.put("icon",colors[r]);


                        }
                    }
                    cache.put(tag,list);
                }
            }

        }
        list = cache.get(tag);

        if(list != null){
            List<JSONObject> guonei = new ArrayList<>();
            List<JSONObject> guowai = new ArrayList<>();
            JSONObject item = null;
            for(int i=0;i<list.size();i++){
                item = list.getJSONObject(i);

                if(item.containsKey("country") && !("cn".equals(item.getString("country")))){
                    guowai.add(item);
                }else{
                    guonei.add(item);
                }
            }
            if(guonei.size() >= 1){
                model.addAttribute("guonei",guonei);
            }
            if(guowai.size() >= 1){
                model.addAttribute("guowai",guowai);
            }
        }
    }

    @Override
    public void cacheContol(HttpServletRequest req, Model model, Boolean forceclear ) {
        super.cacheContol(req, model, forceclear);
        if (forceclear) {
            cache.clear();
        }
        Date date = new Date();
        String szDate = String.format("%tY-%tm-%td", date,date,date);
        if(timeCache.get(szDate) == null){
            cache.clear();

        }else {
            clckTime ++;

            if(clckTime % 50 == 0){
                cache.clear();
            }
        }
    }

}
